/*
 * Copyright (c) 2025 bzy-080408<Bzy080408@outlook.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <bits/stdc++.h>
#include <sys/utsname.h>

#define MAXN 50000
#define MAXM 50000
using namespace std;

map<string, int> name2id;
map<int, string> id2name;
int modulesCnt = 0;
string modulesPath;

struct edgetype
{
    int to;
    int next;
} edge[MAXM];

int head[MAXN];
int outsum[MAXN] = {0};
int edgecnt = 0;

queue<int> sortque;
queue<int> module_load_que;
void AddEdge(int from, int to)
{
    edge[edgecnt].to = to;
    edge[edgecnt].next = head[from];
    head[from] = edgecnt;
    edgecnt++;
    return;
}

string getLinuxVersion()
{
    struct utsname u;
    uname(&u);
    return u.release;
}

string getModulesPath()
{
    string ret = "/lib/modules/";
    ret += getLinuxVersion();
    ret += "/";
    return ret;
}

void CreateMap()
{
    cin.clear();
    string inputfile = modulesPath + "modules.dep";
    freopen(inputfile.data(), "r", stdin);
    string input;
    while (cin >> input)
    {
        input.erase(input.end() - 1);
        name2id[input] = modulesCnt;
        id2name[modulesCnt] = input;
        modulesCnt++;
        char tmp;
        tmp = getchar();
        while(tmp != '\n'){
		    tmp = getchar();//读取下一个字符，为换行符，则break
        }
    }
    return;
}

void ParseIn()
{
    cin.clear();
    string inputfile = modulesPath + "modules.dep";
    freopen(inputfile.data(), "r", stdin);
    // freopen("debug.err", "r", stderr);
    memset(head, -1, sizeof(head));
    string input;
    string root;
    string son;
    while (cin >> input)
    {
        if (input[input.size() - 1] == ':')
        {
            input.erase(input.end() - 1);
            root = input;
        }
        else
        {
            son = input;
            AddEdge(name2id[son], name2id[root]);
            outsum[name2id[root]]++;
        }
    }
    return;
}


void Sort()
{
    int debugsum = 0;
    for (int i = 0; i < modulesCnt; i++)
    {
        if (outsum[i] == 0)
        {
            sortque.push(i);
        }
    }
    while (!sortque.empty())
    {
        for (int i = head[sortque.front()]; i != -1; i = edge[i].next)
        {
            outsum[edge[i].to]--;
            if (outsum[edge[i].to] == 0)
            {
                sortque.push(edge[i].to);
            }
        }
        module_load_que.push(sortque.front());
        sortque.pop();
    }
    return;
}

void load_module()
{
    freopen("debug.log","w",stdout);
    while (!module_load_que.empty())
    {
        string cmd = "insmod " + modulesPath + id2name[module_load_que.front()] ;
        system(cmd.data());
        // cout << cmd << endl;
        module_load_que.pop();
    }
    return;
}

int main()
{
    modulesPath = getModulesPath();
    CreateMap();
    ParseIn();
    Sort();
    load_module();
    return 0;
}